{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.0 64-bit"
  },
  "interpreter": {
   "hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# https://gitee.com/gvp/all\n",
    "\n",
    "\n",
    "import asyncio\n",
    "from pyppeteer import launch\n",
    "\n",
    "browser = await launch(headless=False)  # 关闭无头浏览器\n",
    "page = await browser.newPage()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "await page.goto('https://gitee.com/gvp/all')  # 跳转\n",
    "# await page.screenshot({'path': 'example.png'})  # 截图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "async def find_one(root,selector,property):\n",
    "    result_element = await root.querySelector(selector)\n",
    "    result = await (await result_element.getProperty(property)).jsonValue()\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "cards = await page.querySelectorAll(\".categorical-project-card\")\n",
    "\n",
    "for i,card in enumerate(cards[:10]):\n",
    "    card_content = await find_one(card,\".categorical-project-card > a\",\"href\")\n",
    "    card_desc = await find_one(card,\".project-description\",\"title\")\n",
    "    star = await find_one(card,\"div.project-metas > span:nth-child(1) > span\",\"textContent\")\n",
    "    try:\n",
    "        fork = await find_one(card,\"div.project-metas > span:nth-child(2) > span\",\"textContent\")\n",
    "    except:\n",
    "        fork = \"0\"\n",
    "    print(star,fork,card_content,card_desc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "await browser.close()  # 关闭"
   ]
  }
 ]
}